package com.example.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.pojo.entity.Followers;
import com.example.pojo.entity.Following;
import com.example.pojo.entity.User;
import com.example.pojo.entity.UserProfile;
import com.example.pojo.vo.UserVo;
import com.example.result.Result;
import com.example.service.IFollowersService;
import com.example.service.IFollowingService;
import com.example.service.IUserProfileService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zjw
 * @since 2024-07-16
 */
@RestController
@RequestMapping("/following")
public class FollowingController {
    @Autowired
    private IFollowingService followingService;
    @Autowired
    private IUserProfileService userProfileService;
    @Autowired
    private IFollowersService followersService;

    //根据用户id获取关注列表
    @GetMapping("/id")
    public Result<List<UserVo>> handleGetFollowings(@RequestParam("id") Integer id) {
        try {
            System.out.println(id);
            LambdaQueryWrapper<Following> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Following::getUId, id);
            List<Following> list = followingService.list(lambdaQueryWrapper);
            List<Integer> ids = new ArrayList<>();
            for (Following following1 : list) {
                ids.add(following1.getFriendId());
            }
            List<UserProfile> userProfiles = userProfileService.listByIds(ids);
            List<UserVo> userVos = userProfiles.stream()
                    .map(userProfile -> new UserVo(
                            userProfile.getName(),
                            userProfile.getDescription(),
                            userProfile.getIndexBg(),
                            userProfile.getAvatar()
                            // 继续添加其他需要的属性赋值
                    ))
                    .collect(Collectors.toList());

            return Result.success(userVos);

        } catch (Exception e) {
            return Result.error();

        }

    }

    //点击关注
    @PostMapping("/id")
    public Result handleFollowing(@RequestBody Map<String, String> user) {
       try{
           String uId = user.get("uId");
           String friendId = user.get("friendId");
           Following following = new Following();
           following.setUId(Integer.parseInt(uId));
           following.setFriendId(Integer.parseInt(friendId));
           Followers followers=new Followers();
           followers.setUId(Integer.valueOf(friendId));
           followers.setFriendId(Integer.valueOf(uId));
           followersService.save(followers);
           followingService.save(following);
           return Result.success();
       } catch (Exception e){
           return Result.error();
       }


    }
    //再次点击取关
    @DeleteMapping ("/id")
    public Result handleFollowingDelete(@RequestBody Map<String, String> user) {
        try{
            String uId = user.get("uId");
            String friendId = user.get("friendId");
            LambdaQueryWrapper<Following> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(Following::getUId,uId).eq(Following::getFriendId,friendId);
            followingService.remove(wrapper);
            LambdaQueryWrapper<Followers> wrapper1=new LambdaQueryWrapper<>();
            wrapper1.eq(Followers::getUId,friendId).eq(Followers::getFriendId,uId);
            followersService.remove(wrapper1);
            return Result.success();
        } catch (Exception e){
            return Result.error();
        }


    }


}
